package com.gcstorage.tvdev.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.gcstorage.tvdev.bean.H5AppInfo;
import com.gcstorage.tvdev.constants.ARouterPaths;

import java.util.List;

/**
 * 跳转辅助类
 * created by Sanpark on Date: 2020/6/9 8:59
 */
public class JumpHelper {



    //跳转登陆界面
    public static void gotoLoginActivity() {
        ARouter.getInstance().build(ARouterPaths.TV_APP_LOGIN)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .navigation();
    } //跳转登陆界面
    //跳转H5界面
    public static void gotoH5Activity(Context context, String url, boolean hasTitle, String title) {
        gotoH5Activity(context, url, hasTitle, title, null);
    }

    public static void gotoH5Activity(Context context, String url, boolean hasTitle, String title, H5AppInfo h5AppInfo) {
        Intent intent;
        if ((h5AppInfo != null && h5AppInfo.getAppTitle().contains("NFC")) || url.contains("tar=nfc")) {
//            intent = new Intent(context, WebNfcActivity.class);
        } else {
//            intent = new Intent(context, WebActivity.class);
        }
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.putExtra("hasTitle", hasTitle);
//        intent.putExtra("title", title);
//        intent.putExtra("url", url);
//        if (h5AppInfo != null) {
//            intent.putExtra("h5app", h5AppInfo);
//        }
//        context.startActivity(intent);
//        LiveEventBus.get(EventChannel.EVENT_CHANNEL_NOTICE).post(url);
//        LiveEventBus.get(EventChannel.EVENT_CHANNEL_NOTICE_URL).post(url);
    }

    public static void jumpReflex(Activity act, Bundle bundle, String classPath) {
        try {
            Class cla = Class.forName(classPath);
            jump(act, cla, bundle);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void jump(Activity act, Class actClass, Bundle bundle) {
        Intent intent = new Intent(act, actClass);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        act.startActivity(intent);
    }


    public static void jumpUrl(Activity act, String url, Bundle bundle) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent();
        intent.setData(uri);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        act.startActivity(intent);
    }
    public static void jumpPackageName(Context context, String packageName) {
        // 通过包名获取此APP详细信息，包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (packageinfo == null) {
            ToastUtils.showShort("还未集成该模块，敬请期待");
            return;
        }
        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);

        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = context.getPackageManager()
                .queryIntentActivities(resolveIntent, 0);

        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // packagename = 参数packname
            String packageName1 = resolveinfo.activityInfo.packageName;
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
            String className = resolveinfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // 设置ComponentName参数1:packagename参数2:MainActivity路径
            ComponentName cn = new ComponentName(packageName1, className);

            intent.setComponent(cn);
            context.startActivity(intent);
        }
    }

}

